package com.sky.zero.common.generate.core;

import java.util.List;

public class GenerateService extends GenerateBase {

    private String daoName;

    public GenerateService(GenerateBase generateBase) {
        super(generateBase.packagePath, generateBase.tableName, generateBase.moduleName, generateBase.className,
                generateBase.fieldList, generateBase.fieldMethod);
        daoName = generateBase.className + "Mapper";
    }

    public void execute() {
        StringBuilder builder = new StringBuilder();
        String generatePackage = generatePackage(packagePath + "." + moduleName + ".service");
        String generateClass = generateClass(className);
        String generateAttribute = generateAttribute(fieldList, className);
        builder.append(generatePackage);
        builder.append(
                generateImport(generateImport(generateClass + generateAttribute), packagePath, moduleName, className));
        builder.append(generateClass);
        builder.append(generateAttribute);

        writeGenerateFile(builder.append("}").toString().getBytes(), getGeneratePath("service", "Service"));

    }

    /**
     * 生成引用路径
     **/
    public String generateImport(String importJavaPath, String packagePath, String moduleName, String className) {
        StringBuilder builder = new StringBuilder();
        builder.append(importJavaPath);
        String importPath = packagePath + "." + moduleName;
        builder.append("import ").append(importPath).append(".entity.").append(className).append(";\n");
        builder.append("import ").append(importPath).append(".entity.").append(className).append("Param;\n");
        builder.append("import ").append(importPath).append(".entity.").append(className).append("VO;\n");
        builder.append("import ").append(importPath).append(".mapper.").append(daoName).append(";\n");
        builder.append("\n\n");
        return builder.toString();
    }

    /**
     * 生成类
     **/
    public String generateClass(String className) {
        StringBuilder builder = new StringBuilder();
        builder.append("@Slf4j\n");
        builder.append("@Service\n");
        builder.append("@Transactional(readOnly = true)\n");
        builder.append("public class ").append(className).append("Service extends BaseService {\n\n");
        return builder.toString();
    }

    /**
     * 生成属性和方法
     **/
    public String generateAttribute(List<FieldInfo> fields, String className) {
        StringBuilder b = new StringBuilder();
        b.append("	@Autowired\n");
        b.append("	private ").append(daoName).append(" ").append(lower(daoName)).append(";\n\n");

        // 查询列表(分页)
        b.append("    /** 查询列表(分页)**/\n");
        b.append("    public PageInfo<").append(className).append("VO> get").append(className).append("Page(")
                .append(className).append("Param ").append("param){\n");
        b.append("		param.enablePage();\n");
        b.append("		return toPageInfo(" + lower(className) + "Mapper.findList(param)," + className + "VO.class);\n	}\n\n");

        // 查询列表
        b.append("    /** 查询列表**/\n");
        b.append("    public List<" + className + "VO> get" + className + "List(" + className + "Param param) {\n	");
        b.append("	 return copyToList(" + lower(className) + "Mapper.findList(param), " + className + "VO.class);\n	}\n\n");

        // 根据ID获取数据
        b.append("    /** 根据ID获取详情**/\n");
        b.append("    public " + className + "VO get" + className + "ById(" + fieldInfo.getJavaDataType() + " id) {\n	");
        b.append("	return BeanUtil.toBean(" + lower(className) + "Mapper.findById(id), " + className + "VO.class);\n	}\n\n");


        // 插入数据
        b.append("    /** 新增**/\n");
        b.append("    @Transactional\n");
        b.append("    public Integer insert" + className + "(" + className + " " + lower(className) + ") {\n		");
        b.append("return insertBatch" + className + "(Collections.singletonList(" + lower(className) + "));\n	}\n\n");

        // 批量插入数据
        b.append("    /** 批量新增**/\n");
        b.append("    @Transactional\n");
        b.append("    public Integer insertBatch" + className + "(List<" + className + "> " + lower(className) + "List" + ") {\n		");
        b.append("return " + lower(className) + "Mapper.insertBatch(" + lower(className) + "List" + ");\n	}\n\n");

        // 更新数据
        b.append("    /** 更新**/\n");
        b.append("    @Transactional\n");
        b.append("    public Integer update" + className + "(" + className + " " + lower(className) + ") {\n		");
        b.append("  return updateBatch" + className + "(Collections.singletonList(" + lower(className) + "));\n	}\n\n");

        // 批量更新数据
        b.append("    /** 批量更新**/\n");
        b.append("    @Transactional\n");
        b.append("    public Integer updateBatch" + className + "(List<" + className + "> " + lower(className) + "List" + ") {\n		");
        b.append("  return " + lower(className) + "Mapper.updateBatch(" + lower(className) + "List" + ");\n	}\n\n");

        // 删除数据
        b.append("    /** 根据ID删除**/\n");
        b.append("    @Transactional\n");
        b.append("    public Integer del" + className + "ById(" + fieldInfo.getJavaDataType() + " id) {\n	");
        b.append("  return del" + className + "ByIds(Collections.singletonList(id));\n	}\n\n");

        // 批量删除数据
        b.append("    /** 根据ID批量删除**/\n");
        b.append("    @Transactional\n");
        b.append("    public Integer del" + className + "ByIds(List<" + fieldInfo.getJavaDataType() + "> ids) {\n	");
        b.append("	return " + lower(className) + "Mapper.delByIds(ids);" + "\n	}\n\n");
        return b.toString();
    }

}
